package com.qf.service.impl;

import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 路由策略： 发送队列消息到mq ， 网关项目监听队列消息，调用（三大运营商）执行发送短信的接口
 * 一定是最后一个策略，一定会包含此策略
 * @author YDW
 * projectName:project2202_4
 * time:2022/6/20 22:30
 * description:
 */
@Service("router")
@Slf4j
public class RouterServiceImpl implements FilterChainService {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    AmqpAdmin amqpAdmin;
    @Override
    public boolean strategy(StandardSubmit submit) {
        log.info("[策略模块 -- 路由策略] 策略开始执行！ submit={}",submit);
        String queueName = RabbitMQConstants.TOPIC_SMS_GATEWAY+submit.getGatewayID();
        //创建 持久化（重启后依然存在）队列
        amqpAdmin.declareQueue(QueueBuilder.durable(queueName).build());

        //把submit对象发送到队列
        rabbitTemplate.convertAndSend(queueName,submit);

        log.info("[策略模块 -- 路由策略] 所有策略执行完成！！！");


        return true;
    }
}
